package com.niit.covid.MeiTuanStaterjoin;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JoinReduce extends Reducer<Text,Text,Text,Text> {
    //用来存放 商品编号、商品名称
    List<String> goodsLiset = new ArrayList<>();
    //用来存放 订单编号 实际支付价格
    List<String> orderLiset = new ArrayList<>();

    Text outValue = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        //遍历values
        for (Text value :values){
            if (value.toString().startsWith("goods#")){
                String s = value.toString().split("#")[1];
                goodsLiset.add(s);
            }
        }

        //获取2个集合的长度
        int goodsSize = goodsLiset.size();
        int orderSize = orderLiset.size();

        for (int i = 0; i < orderSize; i++){
            for (int j = 0 ; j < goodsSize ; j ++){
                outValue.set(orderLiset.get(i)+ "t" + goodsLiset.get(j));
                //最终输出：商品id、订单编号、实际支付价格、商品编号、商品名称
                context.write(key,outValue);
            }
        }
        //关闭列表
        orderLiset.clear();
        goodsLiset.clear();
    }
}
